package extloader;

import burp.*;
import window.InitWindow;
import window.MainTab;
import window.utils.customObject;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


public class Extloader implements IBurpExtender,IContextMenuFactory {
    private static IBurpExtenderCallbacks callbacks;
    private PrintWriter stdout;
    private PrintWriter stderr;

    @Override
    public void registerExtenderCallbacks(IBurpExtenderCallbacks iBurpExtenderCallbacks) {
        callbacks = iBurpExtenderCallbacks;

        stdout = new PrintWriter(callbacks.getStdout(), true);
        stderr = new PrintWriter(callbacks.getStderr(), true);

        callbacks.setExtensionName("Extloader");
        callbacks.printOutput("Load successful");
        callbacks.registerContextMenuFactory(this);

        init(callbacks);
    }

    public void init(IBurpExtenderCallbacks iBurpExtenderCallbacks){
        new InitWindow(iBurpExtenderCallbacks);
    }

    @Override
    public List<JMenuItem> createMenuItems(IContextMenuInvocation invocation) {
        ArrayList<JMenuItem> menu_item_list = new ArrayList<JMenuItem>();

        JMenuItem repeaterSendTo = new JMenuItem("Send to Exloader");
        repeaterSendTo.addActionListener(new repeaterSendAction(invocation));
        menu_item_list.add(repeaterSendTo);

        return menu_item_list;
    }

    public class repeaterSendAction implements ActionListener {
        private IContextMenuInvocation invocation;

        public repeaterSendAction(IContextMenuInvocation invocation) {
            this.invocation  = invocation;
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            IHttpRequestResponse[] messages = invocation.getSelectedMessages();
            byte[] req = messages[0].getRequest();
            String request = new String(req);

            MainTab.numPanel.addTab(String.valueOf(MainTab.number), customObject.createNumPanel(request));
            MainTab.numPanel.setTabComponentAt(MainTab.number-1, customObject.createNumTab(MainTab.number));
            MainTab.number++;
        }
    }
}
